/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.service.impl;

import com.egzosn.pay.common.bean.PayMessage;
import com.medusa.gruul.common.core.auth.client.helper.ISecurity;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.handler.Handler;
import com.medusa.gruul.common.core.util.SpringUtils;
import com.medusa.gruul.payment.api.enums.PayType;
import com.medusa.gruul.payment.common.annotation.PayStatusVerifyHanlder;
import com.medusa.gruul.payment.common.enums.TradeStatus;
import com.medusa.gruul.payment.common.helper.PayHelper;
import com.medusa.gruul.payment.service.MultiPayOrderService;
import com.medusa.gruul.payment.service.MultiPayVerifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author 张治保
 * date 2021/12/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MultiPayVerifyServiceImpl implements MultiPayVerifyService {
    private final MultiPayOrderService multiPayOrderService;

    @Override
    public TradeStatus verifyPayStatus(PayType payType, String outTradeNo) {
        Handler<TradeStatus> tradeStatusHandler = SpringUtils.getBean(PayStatusVerifyHanlder.class,payType);
        if (tradeStatusHandler == null){
            throw new ServiceException("系统异常,处理器不存在");
        }
        return tradeStatusHandler.handle(outTradeNo);
    }

    @Override
    public TradeStatus getPayStatus(PayType payType, String outTradeNo) {
        TradeStatus tradeStatus = this.verifyPayStatus(payType, outTradeNo);
        /*
         * 确认支付成功后是否已更新
         */
        if (TradeStatus.SUCCESS == tradeStatus){
            try {
                multiPayOrderService.success(  getMessage(payType, outTradeNo) );
            }catch (Exception ignore){}
        }
        return tradeStatus;
    }


    private PayMessage getMessage(PayType payType, String outTradeNo){
        PayMessage payMessage = new PayMessage(
            new HashMap<String,Object>(6){
                {
                    put("notify",false);
                    put("type",payType.name());
                    put("out_trade_no",outTradeNo);
                }
            }
        );
        payMessage.setPayType(
            PayHelper.wayType(ISecurity.platform().must(), payType).getType()
        );
        return payMessage;
    }
}
